home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 3 / Light ROM 3 - Disc 2.iso / programs / pc / l_parser / lwlo.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  5KB  |  207 lines

  1. /*
  2.     LWLO.C or LWOB.C 
  3.     Copyright (C) 1994  Earl C. Terwilliger
  4.             158 Eades Drive
  5.             Irvine, KY 40336
  6.             Phone (606)-723-5718
  7.             Internet: aisterwi@acs.eku.edu
  8. */
  9. #define BUFSIZE 4096
  10. #include <stdio.h>
  11. #include <fcntl.h>
  12. #include <io.h>
  13. #include <process.h>
  14. #include <stdlib.h>
  15. static unsigned char buf[BUFSIZE];
  16. static unsigned char csize_buf[4];
  17. unsigned long *csize = (long *)csize_buf;
  18. static char debug = 0;
  19. main(argc,argv)
  20.     int argc;
  21.     char *argv[];
  22. {
  23.     int fp,c,d;
  24.     unsigned char form[5], type[5], chunk[5], temp[80], infile[64];
  25.     unsigned int count,length;
  26.     long bread = 0;
  27.     unsigned long points = 0;
  28.     unsigned long tpoints = 0;
  29.     float *x, *y, *z;
  30.     if (argc < 2) syntax(argv[0]);
  31.     strcpy(infile,argv[1]); 
  32.     if (!strchr(argv[1], '.')) strcat(infile, ".OBJ");
  33.     fp = open(infile,O_BINARY|O_RDONLY);
  34.     if (fp == -1) { 
  35.     printf("Unable to open file %s\n",infile); 
  36.     syntax(argv[0]);
  37.     }
  38.     if (argc > 2) {
  39.     if ((argv[2][0] == '-') && (toupper(argv[2][1]) == 'D')) debug = 1;
  40.     }
  41.     read(fp,buf,12);
  42.     bread += 12L;
  43.     get_str(buf,form,4);
  44.     get_str(&buf[8],type,4);
  45.     rev_long(&buf[4],csize_buf);
  46.     if ((c_string(form,"FORM")  != 1) || (c_string(type,"LW")  != 1)) {
  47.       close(fp);
  48.       printf("%s is not a LWLO or LWOB file\n",infile);
  49.       exit(1);
  50.     }
  51.     printf("FORM  = %s\n",form);
  52.     printf("TYPE  = %s  Length = %ld\n",type,*csize);
  53.     while (count = read(fp,buf,8)) {
  54.     bread += (long)count;
  55.     get_str(buf,chunk,4);
  56.     rev_long(&buf[4],csize_buf);
  57.     if (c_string(chunk,"PNTS")) {
  58.          points =  *csize / 12l;
  59.          tpoints = 0l;
  60.          printf("Chunk=%s  Size=%4ld bytes  Points=%ld\n",chunk,*csize,points);
  61.          while(*csize!=0L) {
  62.          count = read(fp,buf,12);
  63.          bread += (long)count;
  64.          *csize -= 12l;
  65.          tpoints += 1l;
  66.          if (debug) { 
  67.              phex(buf,12);
  68.              rev_long(&buf[0],&temp[0]);
  69.              x = (float *)&temp[0];
  70.              rev_long(&buf[4],&temp[4]);
  71.              y = (float *)&temp[4];
  72.              rev_long(&buf[8],&temp[8]);
  73.              z = (float *)&temp[8];
  74.              printf("%03ld x=%12.6f y=%12.6f z=%12.6f\n",tpoints,*x,*y,*z);
  75.          }
  76.          }
  77.          if (tpoints != points) {
  78.          printf("Calculated # of points != points read from file!\n");
  79.          break;
  80.          }
  81.     }
  82.     else {
  83.         printf("Chunk=%s  Size=%4ld bytes\n",chunk,*csize);
  84.         if (*csize > (unsigned long)BUFSIZE) {
  85.         while (*csize!=0L) {
  86.             if (*csize > (long)BUFSIZE) {
  87.             count = read(fp,buf,BUFSIZE);
  88.             *csize -= (long)BUFSIZE;
  89.             }
  90.             else { count = read(fp,buf,(int)*csize); *csize = 0L; }
  91.             bread += (long)count;
  92.         }
  93.         }
  94.         else { 
  95.         count = read(fp,buf,(int)*csize); 
  96.         bread += (long)count; 
  97.         if (c_string(chunk,"SRFS")) {
  98.             c = 0;
  99.             while (c<count) {
  100.             printf("            Surface name %s\n",buf+c);
  101.             d = strlen(buf+c);
  102.             if (d%2) c += d + 1;
  103.             else     c += d + 2;
  104.             }
  105.         }
  106.         else if (c_string(chunk,"SURF")) {
  107.             c = 0;
  108.             printf("            Surface name %s\n",buf);
  109.             d = strlen(buf);
  110.             if (d%2) c += d + 1;
  111.             else     c += d + 2;
  112.             sub_chunk(buf+c,count-c);
  113.              }
  114.         else if (c_string(chunk,"LAYR")) {
  115.             c = 0;
  116.             rev_int(&buf[0]);
  117.             printf("            Layer %2d    Name=%s\n",
  118.                 *(unsigned int *)buf,buf+4);
  119.              }
  120.         }
  121.     }
  122.     }
  123.     printf("%4ld bytes read\n",bread);
  124.     close(fp);
  125.     exit(0);
  126. }
  127. sub_chunk(ptr,lnth)
  128.     unsigned char *ptr;
  129.     unsigned int lnth;
  130. {
  131.     unsigned char *iptr = ptr + 4;    
  132.     unsigned int c;
  133.     char subchunk[5];
  134.     get_str(ptr,subchunk,4);
  135.     rev_int(iptr);
  136.     c = *(unsigned int *)iptr;
  137.     c += 6;
  138.     lnth -= c;
  139.     printf("            Sub Chunk    %s  Length %u\n",subchunk,c);
  140.     if (lnth > 0) sub_chunk(ptr+c,lnth);
  141.     return(0);
  142. }
  143. syntax(ptr)    
  144.     char *ptr;
  145. {
  146.     printf("%s filename[.OBJ] [-d]\n",ptr);
  147.     exit(1);
  148. }
  149. c_string(str1,str2)
  150.     unsigned char *str1;
  151.     unsigned char *str2;
  152. {
  153.     while (1) {
  154.     if ((*str1 == 0) || (*str2 == 0)) return(1);
  155.     if (*str1 != *str2) return(0);
  156.     ++str1;
  157.     ++str2;
  158.      }
  159. }
  160. get_str(where,to,lnth)
  161.     unsigned char *where;
  162.     unsigned char *to;
  163.     int lnth;
  164. {
  165.     while(lnth) {
  166.     *to = *where;
  167.     ++to;
  168.     ++where;
  169.     if (*where == 0) break;
  170.     --lnth;
  171.     }
  172.     *to = 0;
  173.     return(lnth);
  174. }
  175. phex(str,len)
  176.     unsigned char *str;
  177.     int len;
  178. {
  179.     int c = 1;
  180.     while(len) {
  181.     if (!(c%4)) printf("%02X ",*str); else printf("%02X",*str);
  182.     ++str;
  183.     --len;
  184.     ++c;
  185.     }
  186.     printf("\n");
  187.     return(0);
  188. }
  189. rev_long(buf1,buf2)
  190.     unsigned char *buf1, *buf2;
  191. {
  192.     buf2[0] = buf1[3];
  193.     buf2[1] = buf1[2];
  194.     buf2[2] = buf1[1];
  195.     buf2[3] = buf1[0];
  196.     return(0);
  197. }
  198. rev_int(ptr)
  199.     unsigned char *ptr;
  200. {
  201.     unsigned char s;
  202.     s = ptr[0];
  203.     ptr[0] = ptr[1];
  204.     ptr[1] = s;
  205.     return(0);
  206. }
  207.